//============================================================================
// Name        : .cpp
// Author      : subo
// Version     :
// Copyright   : Your copyright notice
// Description : Hello World in C++, Ansi-style
//============================================================================

#include "stdio.h"
#include "inc.h"
#include "unistd.h"
#include "threadpool.h"


CThreadPool pool(100,true,1024);

class CLOGSTDOUT
{

public:
	static CLOGSTDOUT gLogstd;

	CLOGSTDOUT& operator <<(string& str)
	{
		cout << str;
		return *this;
	}
	CLOGSTDOUT& operator <<(const char* str)
	{
		cout << str;
		return *this;
	}

	static CLOGSTDOUT& getinstance()
	{
		return gLogstd;
	}
};

CLOGSTDOUT CLOGSTDOUT::gLogstd;

#define LOG_PRINTF_STDOUT(STRING) CLOGSTDOUT::getinstance()<<STRING

void InitLogPrint()
{
	LogLog::getLogLog()->setInternalDebugging(true);

	try
	{
		ConfigureAndWatchThread configureThread(
				LOG4CPLUS_TEXT("log4cplus.properties"), 5 * 1000);
	} catch (...)
	{
		cout << "Exception..." << endl;
	}

}

CThreadMutex mtx;
int nCnt = 1;
class CTestJob: public CJobBase
{
	string m_strData;
	string m_strResult;
public:
	CTestJob(const char* pcszText)
	{
		m_strData = pcszText;
	}
	CTestJob()
	{
	}

	~CTestJob()
	{

	}
public:
	virtual int Process()
	{
		int nTemp = 0;
		mtx.Lock();
		nTemp = nCnt++;
		mtx.unLock();
		//sleepmillis(1000);
		LOG_PRINT_DEBUG(
				"CTestJob::Process ======> "<<pthread_self()<<"Cnt:===> "<<nTemp);
		return 0;
	}

};

void* startThreadMain(void* param)
{
	int i = 0;
	while (i < 10000)
	{
		CTestJob* pJob = new CTestJob;

		if(pJob != NULL)
		{
			pool.AddTask(pJob);
			i++;
		}
	}

	return NULL;
}

void CreateWorkThread(int nNum)
{

	pthread_t thread_id;
	pthread_attr_t attr;
	pthread_attr_init(&attr);
	pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);

	for (int i = 0; i < nNum; i++)
	{
		pthread_create(&thread_id, NULL, startThreadMain, NULL);
	}
	pthread_attr_destroy(&attr);
}

int main()
{
	InitLogPrint();

	LOG_PRINT_DEBUG("test")

//	CThreadPool::POOLINFO thread;
//	thread.THNREAD_MAXSIZE = 50;
//	thread.THREAD_EXTENTS_SIZE = 10;
//	thread.THREAD_INITSIZE = 25;
//	thread.QUEUE_MAX = 64;
	pool.Init();


	u_int nThreadCnt = 0;

	cout<<"input task number";
	cin>>nThreadCnt;
	CreateWorkThread(nThreadCnt);
	while (sleep((unsigned int) 10000))
		return 0;

}
